package com.example.ams.controller;

import com.example.ams.dto.LeaveRecordDTO;
import com.example.ams.service.LeaveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假记录管理控制器
 * Leave Record Controller
 */
@RestController
@RequestMapping("/api/leaves")
@CrossOrigin(origins = "*")
public class LeaveRecordController {

    @Autowired
    private LeaveRecordService leaveRecordService;

    /**
     * 查询请假记录
     * GET /api/leaves?empId=&from=&to=
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getLeaveRecords(
            @RequestParam(required = false) Long empId,
            @RequestParam(required = false) String from,
            @RequestParam(required = false) String to) {

        try {
            List<LeaveRecordDTO> records = leaveRecordService.findByConditions(empId, from, to);

            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "success");
            response.put("data", records);
            response.put("total", records.size());

            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    /**
     * 根据ID查询请假记录
     * GET /api/leaves/{id}
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getLeaveRecordById(@PathVariable Long id) {
        try {
            LeaveRecordDTO record = leaveRecordService.findById(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "success");
            response.put("data", record);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 新增请假记录
     * POST /api/leaves
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createLeaveRecord(
            @Validated @RequestBody LeaveRecordDTO recordDTO) {
        try {
            LeaveRecordDTO created = leaveRecordService.create(recordDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 201);
            response.put("message", "请假记录创建成功");
            response.put("data", created);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    /**
     * 删除请假记录
     * DELETE /api/leaves/{id}
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteLeaveRecord(@PathVariable Long id) {
        try {
            leaveRecordService.delete(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "请假记录删除成功");
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }
}

